<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="酒店名称" prop="hotelName">
        <el-input
          v-model="queryParams.hotelName"
          placeholder="请输入酒店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="房间价格" prop="price">-->
<!--        <el-input-->
<!--          v-model="queryParams.price"-->
<!--          placeholder="请输入房间价格"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="可用房间数" prop="availableRooms">-->
<!--        <el-input-->
<!--          v-model="queryParams.availableRooms"-->
<!--          placeholder="请输入可用房间数"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeCreateTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeUpdateTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['boot:hotel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['boot:hotel:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['boot:hotel:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hotelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="酒店ID" align="center" prop="hotelId" />
      <el-table-column label="酒店名称" align="center" prop="hotelName" :show-overflow-tooltip="true"/>
      <el-table-column label="省份" align="center" prop="province" :show-overflow-tooltip="true"/>
      <el-table-column label="城市" align="center" prop="city" :show-overflow-tooltip="true"/>
      <el-table-column label="酒店地址" align="center" prop="hotelAddress" :show-overflow-tooltip="true"/>
      <el-table-column label="酒店电话" align="center" prop="hotelPhone" :show-overflow-tooltip="true"/>
      <el-table-column label="房间类型" align="center" prop="roomType" :show-overflow-tooltip="true"/>
      <el-table-column label="房间价格" align="center" prop="price" :show-overflow-tooltip="true"/>
      <el-table-column label="可用房间数" align="center" prop="availableRooms" width="104" :show-overflow-tooltip="true"/>
      <el-table-column label="酒店图片" align="center" prop="hotelImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.hotelImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" :show-overflow-tooltip="true"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="90px" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['boot:hotel:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['boot:hotel:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改酒店信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="hotelRef" :model="form" :rules="rules">
        <el-form-item label="酒店名称" prop="hotelName">
          <el-input v-model="form.hotelName" placeholder="请输入酒店名称" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input
            v-model="form.province"
            placeholder="请输入省份"
          />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input
            v-model="form.city"
            placeholder="请输入城市"
          />
        </el-form-item>
        <el-form-item label="酒店地址" prop="hotelAddress">
          <el-input v-model="form.hotelAddress" placeholder="请输入酒店地址" />
        </el-form-item>
        <el-form-item label="酒店电话" prop="hotelPhone">
          <el-input v-model="form.hotelPhone" placeholder="请输入酒店电话" />
        </el-form-item>
        <el-form-item label="房间类型" prop="roomType">
          <el-input v-model="form.roomType" placeholder="请输入房间类型" />
        </el-form-item>
        <el-form-item label="房间价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入房间价格" />
        </el-form-item>
        <el-form-item label="可用房间数" prop="availableRooms">
          <el-input v-model="form.availableRooms" placeholder="请输入可用房间数" />
        </el-form-item>
        <el-form-item label="酒店图片" prop="hotelImage">
          <image-upload v-model="form.hotelImage"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Hotel">
import { listHotel, getHotel, delHotel, addHotel, updateHotel, getCitiesByProvince } from "@/api/boot/hotel"

const { proxy } = getCurrentInstance()

const hotelList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const daterangeCreateTime = ref([])
const daterangeUpdateTime = ref([])
const cityList = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hotelName: null,
    province: null,
    city: null,
    price: null,
    availableRooms: null,
    createTime: null,
    updateTime: null,
  },
  rules: {
    hotelId: [
      { required: true, message: "酒店ID不能为空", trigger: "blur" }
    ],
    hotelName: [
      { required: true, message: "酒店名称不能为空", trigger: "blur" }
    ],
    hotelAddress: [
      { required: true, message: "酒店地址不能为空", trigger: "blur" }
    ],
    roomType: [
      { required: true, message: "房间类型不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "房间价格不能为空", trigger: "blur" }
    ],
    availableRooms: [
      { required: true, message: "可用房间数不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询酒店信息列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0]
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1]
  }
  if (null != daterangeUpdateTime && '' != daterangeUpdateTime) {
    queryParams.value.params["beginUpdateTime"] = daterangeUpdateTime.value[0]
    queryParams.value.params["endUpdateTime"] = daterangeUpdateTime.value[1]
  }
  listHotel(queryParams.value).then(response => {
    hotelList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    hotelId: null,
    hotelName: null,
    province: null,
    city: null,
    hotelAddress: null,
    hotelPhone: null,
    roomType: null,
    price: null,
    availableRooms: null,
    hotelImage: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("hotelRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = []
  daterangeUpdateTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.hotelId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加酒店信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _hotelId = row.hotelId || ids.value
  getHotel(_hotelId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改酒店信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hotelRef"].validate(valid => {
    if (valid) {
      if (form.value.hotelId != null) {
        updateHotel(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addHotel(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _hotelIds = row.hotelId || ids.value
  proxy.$modal.confirm('是否确认删除酒店信息编号为"' + _hotelIds + '"的数据项？').then(function() {
    return delHotel(_hotelIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 省份变化处理 */
function handleProvinceChange(province) {
  queryParams.value.city = null
  cityList.value = []
  if (province) {
    getCitiesByProvince(province).then(response => {
      cityList.value = response.data
    })
  }
}

getList()
</script>
